package net.borlis.sample.web.context;

import net.borlis.configuration.common.ApplicationConfiguration;
import net.borlis.configuration.context.tt.ContextFile;
import net.borlis.sample.services.facade.api.IDataProducer;
import net.borlis.errors.exceptions.TranslatedException;
import net.borlis.errors.providers.common.ITranslatedExceptionProcessor;
import net.borlis.errors.providers.implementation.filebase.LocaleFileErrors;
import net.borlis.log.SimpleLogger;
import net.borlis.web.auth.AuthenticationClientKeyException;
import net.borlis.web.auth.ClientKeyChecker;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.logging.Logger;

/**
 * The weld context of the application.
 *
 * @author alex@borlis.net
 */
@ApplicationScoped
public class DynamixContext implements WeldWebContext {

    @Inject @ContextFile
    private ApplicationConfiguration configuration;

    @Inject
    private IDataProducer dataProducer;

    @Inject @ContextFile
    private ClientKeyChecker clientKeyChecker;

    @Inject @LocaleFileErrors
    private ITranslatedExceptionProcessor translatedExceptionProcessor;

    @Inject
    private Logger logger;

    @PostConstruct
    public void init() {
        logger.info("======= the configuration reading =======");
        logger.info("database server = " + configuration.getServer() + ":" + configuration.getPort());
        logger.info("database name = " + configuration.getSchemaVersion());
        logger.info("database login = " + configuration.getJdbcUser());
        logger.info("database auto update = " + configuration.getAutoUpdate());
        logger.info("database server = " + configuration.getServer() + ":" + configuration.getPort());
        logger.info("encryption login = " + configuration.getEncryptionServiceLogin());
        logger.info("encryption url = " + configuration.getEncryptionServiceURL());
        logger.info("======= ======================= =======");
    }

    @SimpleLogger
    public IDataProducer getDataProducer() {
        return dataProducer;
    }

    @SimpleLogger
    public void verifyClientKey(String clientKey, String locale) /* throws TranslatedException */ {
        try {
            clientKeyChecker.check(clientKey);
        } catch (AuthenticationClientKeyException e){
            throw translatedExceptionProcessor.newTranslatedException(locale, "401", e);
        }
    }

    @SimpleLogger
    public TranslatedException generateException(String locale, String code){
        return translatedExceptionProcessor.newTranslatedException(locale, code);
    }

    @SimpleLogger
    public TranslatedException generateException(String locale, String code, Exception e) {
        return translatedExceptionProcessor.newTranslatedException(locale, code, e);
    }

    @SimpleLogger
    public ApplicationConfiguration getConfiguration() {
        return configuration;
    }

    @Override @SimpleLogger
    public Boolean isInit() {
        return dataProducer!=null && clientKeyChecker!=null && translatedExceptionProcessor!=null;
    }

}
